home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / pvm34b3.zip / pvm34b3 / pvm3 / examples / lmbi.c < prev    next >
C/C++ Source or Header  |  1997-07-22  |  7KB  |  209 lines

  1.  
  2. static char rcsid[] =
  3.     "$Id: lmbi.c,v 1.4 1997/07/09 13:25:08 pvmsrc Exp $";
  4.  
  5. /*
  6.  *         PVM version 3.4:  Parallel Virtual Machine System
  7.  *               University of Tennessee, Knoxville TN.
  8.  *           Oak Ridge National Laboratory, Oak Ridge TN.
  9.  *                   Emory University, Atlanta GA.
  10.  *      Authors:  J. J. Dongarra, G. E. Fagg, M. Fischer
  11.  *          G. A. Geist, J. A. Kohl, R. J. Manchek, P. Mucci,
  12.  *         P. M. Papadopoulos, S. L. Scott, and V. S. Sunderam
  13.  *                   (C) 1997 All Rights Reserved
  14.  *
  15.  *                              NOTICE
  16.  *
  17.  * Permission to use, copy, modify, and distribute this software and
  18.  * its documentation for any purpose and without fee is hereby granted
  19.  * provided that the above copyright notice appear in all copies and
  20.  * that both the copyright notice and this permission notice appear in
  21.  * supporting documentation.
  22.  *
  23.  * Neither the Institutions (Emory University, Oak Ridge National
  24.  * Laboratory, and University of Tennessee) nor the Authors make any
  25.  * representations about the suitability of this software for any
  26.  * purpose.  This software is provided ``as is'' without express or
  27.  * implied warranty.
  28.  *
  29.  * PVM version 3 was funded in part by the U.S. Department of Energy,
  30.  * the National Science Foundation and the State of Tennessee.
  31.  */
  32.  
  33. /*
  34.  * Filename: lmbi.c
  35.  *
  36.  * This program will "populate" the mailbox with a number of entries
  37.  * so that another program ( gmbi.c ) may then retrieve these entries 
  38.  * using the pvm_getmboxinfo() function.
  39.  *
  40.  * Flags will be set the same for all instances started.
  41.  *
  42.  * usage: lmbi #instances <flags> <exit_status )
  43.  *
  44.  *     flags:
  45.  *         PvmMboxDefault         d
  46.  *         PvmMboxPersistent      p
  47.  *         PvmMboxMultiInstance   m
  48.  *         PvmMboxOverWritable    o
  49.  *
  50.  *     exit_status:
  51.  *         wait for "end" message        w
  52.  *         exit when finished loading    e  ( see note 1 )
  53.  *
  54.  *  Note 1:
  55.  *  -------
  56.  *      Unless the "p" flag is set - all entries to the maibox
  57.  *      created by this run will be removed when this task exits.
  58.  *      So, only when "p" is set should the "e" exit status be used.
  59.  *      Unless you wnat the mailbox entries to be created and then 
  60.  *      immediately removed.
  61.  *
  62.  *      Remember that with "p" and "e" flag set that the tid will
  63.  *      become 0 for the associated mailbox entry( s ) since the "owner"
  64.  *      task will no longer exist. -- this is normal and expected...
  65.  *
  66.  *  files used:  lmbi.c taskf.c
  67.  */
  68.  
  69. #include <stdio.h>
  70. #ifndef WIN32
  71. #include <unistd.h>        /* for gethostname */
  72. #else
  73. #include "pvmwin.h"
  74. #endif
  75. #include <string.h>
  76. #include "pvm3.h"
  77.  
  78. main( argc, argv )
  79. int argc;
  80. char *argv[];
  81. {
  82.     char *me = "lmbi";
  83.     char machine[25];
  84.     char message[250];
  85.     int mytid;
  86.     char service_name[25];
  87.     int i;
  88.     int index;                /* returned index for mailbox entry */
  89.  
  90.     char flagc[10];            /* hold characters for flags */
  91.     int flags;                /* PvmMbox flags set */
  92.     char *flagstring;        /* pointer to string of flag settings */
  93.  
  94.     int their_tid;            /* tid of task sending service request */
  95.     char msg_txt[100];        /* message received as service request */
  96.     int msg_buf;            /* message buffer to receive on */
  97.  
  98.     int entries = 0;        /* # of entries to load into mailbox db */
  99.     char exit_status;        /* incomming exit_status flag value */
  100.     int context;            /* context of this message */
  101.  
  102.     /* display_incomming_parameters( me, argc, argv ); */
  103.  
  104.     /*
  105.      *  validate input parameters
  106.      */
  107.     if ( argc == 4 ) {
  108.         /* correct quantity - anyway... */
  109.         entries = atoi( argv[1] );    /* number of entries to insert */
  110.         strcpy( flagc, argv[2] );    /* copy flags to local */
  111.         exit_status = argv[3][0];    /* grab only the 1st character */
  112.         if ( exit_status != 'w' && exit_status != 'e' ) {
  113.             /* unknown exit_status entered - complain and exit */
  114.             printf( "\n\nInvalid exit_status value <%c> entered.\n" );
  115.             printf( "\texit_status:\n" );
  116.             printf( "\t\twait for end message\t\tw\n" );
  117.             printf( "\t\texit when finished loading\te\n" );
  118.             exit( -1 );
  119.         }
  120.     }
  121.     else {
  122.         /* unknown input sequence - complain and exit */
  123.         printf( "\n\nusage: lmbi #instances <flags> <exit_status>\n" );
  124.         printf( "\tflags:\n" );
  125.         printf( "\t\tPvmMboxDefault\t\td\n" );
  126.         printf( "\t\tPvmMboxPersistent\tp\n" );
  127.         printf( "\t\tPvmMboxMultiInstance\tm\n" );
  128.         printf( "\t\tPvmMboxOverWritable\to\n" );
  129.         printf( "\texit_status:\n" );
  130.         printf( "\t\twait for end message\t\tw\n" );
  131.         printf( "\t\texit when finished loading\te\n" );
  132.         exit( -1 );
  133.     }
  134.  
  135.     printf( "%s: sending <%s> to set_flags.\n", me, flagc );
  136.     if ( ( flags = set_flags( &flagc, &flagstring ) ) < 0 ) {
  137.         exit( -1 );    /* something failed - abort! */
  138.     }
  139.  
  140.     printf( "%s: create %d mailbox instances ", me, entries );
  141.     printf( "with flags set to <%d> : <%s>.\n", flags, flagstring );
  142.  
  143.     if ( ( mytid = pvm_mytid() ) == PvmSysErr ) {
  144.         printf( "\nPVM not up!\n" );
  145.         exit( -1 );
  146.     }
  147.  
  148.     gethostname( machine, 25 );
  149.  
  150.     printf( "%s: t%x on machine <%s> with context %d.\n\n",
  151.             me, mytid, machine, pvm_getcontext() );
  152.  
  153.     /*
  154.      * create and insert the mailbox entries
  155.      */
  156.     for ( i=0 ; i < entries ; i++ )
  157.     {
  158.         /*
  159.          *  create message to place in mailbox
  160.          */
  161.         context = pvm_getcontext();
  162.         sprintf( service_name, "t%x%s%d", mytid, me, i );
  163.         sprintf( message,
  164.                 "%s: service_name %s   flags %d = %s   machine %s   tid t%x   context %d", 
  165.                 me, service_name, flags, flagstring, machine, mytid,
  166.                 context );
  167.         pvm_initsend( PvmDataDefault );
  168.         pvm_packf( "%+ %d %d %s %s",
  169.                 PvmDataDefault, mytid, context, machine, message );
  170.         if ( (index = pvm_putinfo( service_name, pvm_getsbuf(), flags ))
  171.                 == PvmExists ) {
  172.             printf( "can't register - service already running\n" );
  173.             exit( -1 );
  174.         }
  175.         printf( "%s: just inserted entry <%s> at index %d.\n",
  176.                 me, service_name, index );
  177.         printf( "\tmessage inserted: %s\n", message );
  178.     } /* end_for */
  179.  
  180.     /*
  181.      *  Decide if should hang around and wait for another task to shut
  182.      *  me down or if I should simply exit on my own.
  183.      *  This decision is important in the case of using ( or not ) the
  184.      *  persistent flag as mailbox entries without the persistent flag
  185.      *  marked will be removed when their creating task exits.
  186.      *  Thus must either mark as persistent or hang around if I want
  187.      *  to look at or keep around these mailbox entries.
  188.      */
  189.     if ( exit_status == 'w' ) {
  190.         for ( ; ; )
  191.         {
  192.             /*
  193.              *  wait for exit request
  194.              */
  195.             printf("%s: t%x on machine <%s> with context %d",
  196.                 me, mytid, machine, pvm_getcontext());
  197.             printf(" - waiting for exit request.\n");
  198.             msg_buf = pvm_recv( -1, -1 );
  199.             pvm_bufinfo( msg_buf, (int *) 0, (int *) 0, &their_tid );
  200.             pvm_upkstr( msg_txt );
  201.             printf( "%s: t%x %s.\n", me, mytid, msg_txt );
  202.             if ( strncmp( msg_txt, "END", 3 ) == 0 ) break;
  203.         } /* end_for */
  204.     } /* end_if */
  205.     pvm_exit();
  206.     exit( 0 );
  207. }
  208.  
  209.